package cn.stylefeng.guns.modular.agv.controller;

import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.agv.entity.AgvAreaQuota;
import cn.stylefeng.guns.modular.agv.model.params.AgvAreaQuotaParam;
import cn.stylefeng.guns.modular.agv.service.AgvAreaQuotaService;
import cn.stylefeng.guns.modular.basic.entity.BasicWarehouse;
import cn.stylefeng.guns.modular.basic.model.params.BasicAreaParam;
import cn.stylefeng.guns.modular.basic.model.result.BasicAreaResult;
import cn.stylefeng.guns.modular.basic.service.BasicAreaService;
import cn.stylefeng.guns.modular.basic.service.BasicWarehouseService;
import cn.stylefeng.guns.sys.modular.system.entity.User;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import cn.stylefeng.guns.sys.modular.system.service.UserService;
import cn.stylefeng.roses.core.util.ToolUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;


/**
 * agv库区限制控制器
 *
 * @author zhangzh
 * @Date 2025-07-08 10:10:15
 */
@Controller
@RequestMapping("/agvAreaQuota")
public class AgvAreaQuotaController extends BaseController {

    private String PREFIX = "/agv/agvAreaQuota";

    @Autowired
    private AgvAreaQuotaService agvAreaQuotaService;

    @Autowired
    private UserService userService;

    @Autowired
    private BasicWarehouseService basicWarehouseService;

    @Autowired
    private BasicAreaService basicAreaService;

    /**
     * 跳转到主页面
     *
     * @author zhangzh
     * @Date 2025-07-08
     */
    @RequestMapping("")
    public String index(Model model) {
        User user = userService.getcurrentUser();
        Long deptId = user.getDeptId();
        Long deliverId = user.getDeliverId();
        //查询可选择仓库
        model.addAttribute("basicWarehouseList", basicWarehouseService.findAvailableList(deptId,deliverId,user.getOperateId()));
        return PREFIX + "/agvAreaQuota.html";
    }

    /**
     * 新增页面
     *
     * @author zhangzh
     * @Date 2025-07-08
     */
    @RequestMapping("/add")
    public String add(Model model) {
        User user = userService.getcurrentUser();
        Long deptId = user.getDeptId();
        Long deliverId = user.getDeliverId();
        //查询可选择仓库
        model.addAttribute("basicWarehouseList", basicWarehouseService.findAvailableList(deptId,deliverId,user.getOperateId()));
        return PREFIX + "/agvAreaQuota_add.html";
    }

    /**
     * 编辑页面
     *
     * @author zhangzh
     * @Date 2025-07-08
     */
    @RequestMapping("/edit")
    public String edit(Model model,Long id) {
        User user = userService.getcurrentUser();
        Long deptId = user.getDeptId();
        Long deliverId = user.getDeliverId();
        //查询可选择仓库
        model.addAttribute("basicWarehouseList", basicWarehouseService.findAvailableList(deptId,deliverId,user.getOperateId()));
        // 根据ID获取数据
        List<BasicAreaResult> basicAreaList = new ArrayList<>();
        AgvAreaQuota agvAreaQuota = agvAreaQuotaService.lambdaQuery().eq(AgvAreaQuota::getId, id).one();
        if(ToolUtil.isNotEmpty(agvAreaQuota) && ToolUtil.isNotEmpty(agvAreaQuota.getWarehouseId())){
            BasicAreaParam param = new BasicAreaParam();
            param.setAreaWarehouseId(agvAreaQuota.getWarehouseId().intValue());
            basicAreaList = this.basicAreaService.findListBySpec(param,null,null);
        }
        model.addAttribute("basicAreaList", basicAreaList);
        return PREFIX + "/agvAreaQuota_edit.html";
    }

    /**
     * 新增接口
     *
     * @author zhangzh
     * @Date 2025-07-08
     */
    @RequestMapping("/addItem")
    @ResponseBody
    public ResponseData addItem(AgvAreaQuotaParam agvAreaQuotaParam) {
        User user = userService.getcurrentUser();
        if(ToolUtil.isNotEmpty(agvAreaQuotaParam.getWarehouseId())){
            BasicWarehouse basicWarehouse = basicWarehouseService.getById(agvAreaQuotaParam.getWarehouseId());
            if(ToolUtil.isNotEmpty(basicWarehouse)){
                agvAreaQuotaParam.setWarehouseName(basicWarehouse.getWarehouseName());
            }
        }
        agvAreaQuotaParam.setDeptId(user.getDeptId());
        this.agvAreaQuotaService.add(agvAreaQuotaParam);
        return ResponseData.success();
    }

    /**
     * 编辑接口
     *
     * @author zhangzh
     * @Date 2025-07-08
     */
    @RequestMapping("/editItem")
    @ResponseBody
    public ResponseData editItem(AgvAreaQuotaParam agvAreaQuotaParam) {
        if(ToolUtil.isNotEmpty(agvAreaQuotaParam.getWarehouseId())){
            BasicWarehouse basicWarehouse = basicWarehouseService.getById(agvAreaQuotaParam.getWarehouseId());
            if(ToolUtil.isNotEmpty(basicWarehouse)){
                agvAreaQuotaParam.setWarehouseName(basicWarehouse.getWarehouseName());
            }
        }
        this.agvAreaQuotaService.update(agvAreaQuotaParam);
        return ResponseData.success();
    }

    /**
     * 删除接口
     *
     * @author zhangzh
     * @Date 2025-07-08
     */
    @RequestMapping("/delete")
    @ResponseBody
    public ResponseData delete(AgvAreaQuotaParam agvAreaQuotaParam) {
        this.agvAreaQuotaService.delete(agvAreaQuotaParam);
        return ResponseData.success();
    }

    /**
     * 查看详情接口
     *
     * @author zhangzh
     * @Date 2025-07-08
     */
    @RequestMapping("/detail")
    @ResponseBody
    public ResponseData detail(AgvAreaQuotaParam agvAreaQuotaParam) {
        AgvAreaQuota detail = this.agvAreaQuotaService.getById(agvAreaQuotaParam.getId());
        return ResponseData.success(detail);
    }

    /**
     * 查询列表
     *
     * @author zhangzh
     * @Date 2025-07-08
     */
    @ResponseBody
    @RequestMapping("/list")
    public LayuiPageInfo list(AgvAreaQuotaParam agvAreaQuotaParam,String condition ) {
        User user = userService.getcurrentUser();
        agvAreaQuotaParam.setDeptId(user.getDeptId());
        return this.agvAreaQuotaService.findPageBySpec(agvAreaQuotaParam,condition);
    }

}


